package com.example.recycler.rxjava20;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    private TextView mTvCenter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTvCenter = (TextView) findViewById(R.id.tv_center);

    }

    public void go1(View v) {
        Observer<String> observer=new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                System.out.println("MainActivity.onSubscribe");
            }

            @Override
            public void onNext(String s) {
                System.out.println("MainActivity.onNext:"+s);
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("MainActivity.onError"+e.getMessage());
            }

            @Override
            public void onComplete() {
                System.out.println("MainActivity.onComplete");
            }
        };
        Observable<String> observable=Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e/*事件发射器*/) throws Exception {
                    e.onNext("尼禄花嫁");
                    e.onNext("两仪式");
                    e.onNext("倾国倾城黑贞德");
                    e.onComplete();
            }
        });
        observable.subscribe(observer);

    }

    public void go2(View v) {
            Observable.create(new ObservableOnSubscribe<String>() {
                @Override
                public void subscribe(ObservableEmitter<String> e) throws Exception {
                    e.onNext("一个两仪式");
                    e.onNext("两个两仪式");
                    e.onNext("三个两仪式");
                    e.onComplete();
                }
            }).map(new Function<String, String>() {
                @Override
                public String apply(@NonNull String s) throws Exception {
                    return "签下契约"+s;
                }
            }).subscribe(new Consumer<String>() {
                @Override
                public void accept(@NonNull String s) throws Exception {
                    Log.e("召唤出了:",s);
                }
            });

    }

    public void go3(View v) {
        Toast.makeText(this, "flatMap", Toast.LENGTH_SHORT).show();
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("一个两仪式");
                e.onNext("两个两仪式");
                e.onNext("三个两仪式");
                e.onNext("差一满宝的两仪式");
                e.onComplete();
            }
        }).flatMap(new Function<String, ObservableSource<String>>() {
            @Override
            public ObservableSource<String> apply(@NonNull String s) throws Exception {
                List<String> fgo=new ArrayList<String>();
                for (int i = 0; i < 3; i++) {
                    fgo.add("相遇："+s);
                }

                return Observable.fromIterable(fgo)
                        .delay(10, TimeUnit.MILLISECONDS);

            }
        }).subscribe(new Consumer<String>() {
            @Override
            public void accept(@NonNull String s) throws Exception {
                Log.e("召唤出了:",s);
            }
        });


    }

    public void go4(View v) {
        Toast.makeText(this, "ZIP操作", Toast.LENGTH_SHORT).show();
        Observable<String> observable1=Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("一个两仪式");
                e.onNext("两个两仪式");
                e.onNext("三个两仪式");
                e.onNext("差一满宝的两仪式");
                e.onComplete();
            }
        }).subscribeOn(Schedulers.io());

        Observable<String> observable2=Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("一个式姐");
                e.onNext("两个式姐");
                e.onNext("获得了三个式姐");
                e.onComplete();
            }
        }).subscribeOn(Schedulers.io());

        Observable.zip(observable1, observable2, new BiFunction<String, String, String>() {

            @Override
            public String apply(@NonNull String s, @NonNull String s2) throws Exception {
                return s+s2;
            }
        }).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.e("fate","onSubscribe");
            }

            @Override
            public void onNext(String s) {
                Log.e("fate","契约召唤到了"+s);
            }

            @Override
            public void onError(Throwable e) {
                Log.e("fate","onError");
            }

            @Override
            public void onComplete() {
                Log.e("fate","onComplete");
            }
        });
    }
}
